Safe lazy initialization of class properties#16756
Draft
ysbaddaden wants to merge 4 commits intocrystal-lang:masterfrom
Draft
Safe lazy initialization of class properties#16756ysbaddaden wants to merge 4 commits intocrystal-lang:masterfrom
ysbaddaden wants to merge 4 commits intocrystal-lang:masterfrom
Conversation
Merged
straight-shoota
pushed a commit
that referenced
this pull request
Mar 24, 2026
Always checked alternative to `Sync::Mutex` and `Sync::RWLock` in a single and significantly smaller type (no crystal type id, no lock type, no reentrancy counter) at 24 bytes instead of 40 bytes. The API is also limited to `#lock(&)` and `#rlock(&)` and it raises on deadlock. Extracted from #16756. NOTE: this is for specific internal usages where we want simple safety; we won't need an unchecked lock (use `Sync::MU` directly) or a reentrant lock (use `Sync::Mutex` or `Sync::RWLock`) types.
Collaborator
Author
|
Back to DRAFT. We need to think deeper about the issue. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Wraps the lazy initialization of a class variables in a rwlock so the variable will only be (re)initialized once when it's
nil.gettermacro #15556).Uses a simple lock instead of trying to reuse crystal/once that led to the variable not being reinitialized when reset back to
nil.TODO:
Crystal::LockAddCrystal::Lock#16768ISSUE: it's still parallel unsafe because the
class_setterwill mutate the class variable without synchronization and the class variable might be a mixed union and thus unsafe to store/load (see #15085)... but the setter doesn't know if there's (or will be) a lazy initialization. I could fixclass_property(&)to have the setter synchronize... but it wouldn't be equivalent toclass_getter(&)+class_setterthat would still be unsafe.I believe we must start fixing #15085 for class variables so we can at least trust that
(value = @@foo).nil?and@@foo = valueare thread safe.Closes #14905
Alternative to #15548 (reverted)